package com.coocaa.ops.admin.v1.panel.biz.impl;

import com.coocaa.ops.admin.v1.panel.biz.IBlockPictureRlsBiz;
import com.coocaa.ops.admin.v1.panel.mapper.IBlockPictureRlsMapper;
import com.coocaa.ops.admin.v1.poster.biz.impl.GlobalPictureBizImpl;
import com.coocaa.ops.common.core.base.BaseBizImpl;
import com.coocaa.ops.common.core.base.mybatis.QueryTemplate;
import com.coocaa.ops.common.core.biz.panel.entity.BlockContentInfoEntity;
import com.coocaa.ops.common.core.biz.panel.entity.BlockPictureRlsEntity;
import com.coocaa.ops.common.core.biz.poster.entity.GlobalPictureEntity;
import com.coocaa.ops.common.tool.base.InstanceUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author bjh
 * @description 推荐位背景图等海报关联表-biz层接口实现
 * @date 2021-02-05 14:32:38
 */
@Service
public class BlockPictureRlsBizImpl extends BaseBizImpl<IBlockPictureRlsMapper, BlockPictureRlsEntity> implements IBlockPictureRlsBiz {
    @Autowired
    private GlobalPictureBizImpl globalPictureBiz;

    @Override
    public void handleSaveBlockPictureRlsList(List<BlockContentInfoEntity> blockContentInfoEntityList) {
        List<BlockPictureRlsEntity> saveEntityList = InstanceUtil.newArrayList();
        for (BlockContentInfoEntity blockContentInfoEntity : blockContentInfoEntityList) {
            handleEachBlockContentInfoEntity(blockContentInfoEntity, saveEntityList);
        }
        saveBatchAvoidEmpty(saveEntityList);
    }

    @Override
    public List<BlockPictureRlsEntity> queryByBlockId(Integer blockId) {
        QueryTemplate<BlockPictureRlsEntity> queryTemplate = QueryTemplate.getEqQueryTemplate("flag", 1);
        queryTemplate.lambda().eq(BlockPictureRlsEntity::getBlockId, blockId);
        List<BlockPictureRlsEntity> blockPictureRlsEntityList = getMapper().selectList(queryTemplate);
        if (CollectionUtils.isEmpty(blockPictureRlsEntityList)) {
            return blockPictureRlsEntityList;
        }
        for (BlockPictureRlsEntity blockPictureRlsEntity : blockPictureRlsEntityList) {
            GlobalPictureEntity globalPictureEntity = globalPictureBiz.queryById(blockPictureRlsEntity.getPictureId());
            blockPictureRlsEntity.setGlobalPicture(globalPictureEntity);
        }
        return blockPictureRlsEntityList;
    }

    private void handleEachBlockContentInfoEntity(BlockContentInfoEntity blockContentInfoEntity, List<BlockPictureRlsEntity> saveEntityList) {
        List<BlockPictureRlsEntity> blockPictureRlsEntityList = blockContentInfoEntity.getBlockPictureRlsList();
        if (CollectionUtils.isEmpty(blockPictureRlsEntityList)) {
            return;
        }
        for (BlockPictureRlsEntity blockPictureRlsEntity : blockPictureRlsEntityList) {
            GlobalPictureEntity globalPictureEntity = blockPictureRlsEntity.getGlobalPicture();
            if (null == globalPictureEntity || StringUtils.isBlank(globalPictureEntity.getPictureUrl())) {
                continue;
            }
            globalPictureEntity = globalPictureBiz.queryOrSavePoster(globalPictureEntity.getPictureUrl(), blockContentInfoEntity.getModifier());
            blockPictureRlsEntity.setPictureId(globalPictureEntity.getId());
            blockPictureRlsEntity.setBlockId(blockContentInfoEntity.getId());
            saveEntityList.add(blockPictureRlsEntity);
        }
    }
}
